import axios from 'axios'
import store from "@/store"
import router from "@/router"
import {Message} from "element-ui"
export const baseURL='http://big-event-vue-api-t.itheima.net'
const myAxios=axios.create({
  baseURL:baseURL
})

myAxios.interceptors.request.use((config)=>{
              if(store.state.token)
                  config.headers.Authorization=store.state.token
              return config
          },
          (error)=>{
            return Promise.reject(error)
          }
  )

myAxios.interceptors.response.use((config)=>{
  return config
},(error)=>{
  if(error.response.status==401){
    //token过期引起的错误
    //清除vuex中的token与用户信息
    store.commit("updateUserInfo",{})
    store.commit("updateToken","")
    //跳转到登录页
    Message({
      type:"error",
      message:"token过期，请重新登录"
    })
    router.push("/login")
  }
  return PromiseRejectionEvent.reject(error)
})

export default myAxios
